<?php

//Quick tests about the datamodel system


//start session
session_start();

//load the data model
include_once "datamodel/BabiliLoadDataModel.php";



/*********** LANGUAGES TEST **********/

echo "<h2>Languages TESTS : </h2>
<br />
<ul>";
	//Insert
	echo "<li>Insert Italian : ";
	
	$id_italian = $babili_language->add('italian', 'Italy', 'IT', 'it_IT', 'img/flag/it.gif');
	if($id_italian){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	//get italian infos
	echo "<li>Get Italian infos (with id) : ";
	
	$italian_info = $babili_language->get($id_italian);
	if(isset($italian_info['name']) && $italian_info['name'] == 'italian'){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Get Itlian infos (with ISO code 'IT') : ";
	
	$italian_info = $babili_language->get('IT');
	if(isset($italian_info['name']) && $italian_info['name'] == 'italian'){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Get all languages : ";
	$languages = $babili_language->get_all();
	if(is_array($languages) && count($languages) > 0){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Delete Italian : ";
	if($babili_language->delete($id_italian)){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
echo '</ul>';
	
	
/*********** USERS AND PERMISSIONS TEST **********/
echo "<h2>Users TESTS : </h2>
<br />
<ul>";
	//Insert
	echo "<li>Insert Test user : ";
	$user_id = $babili_users->create('test@babili.net', md5('test'));
	if($user_id){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Modify a user (Change password) : ";
	if($babili_users->modify($user_id, null, md5('hello'))){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Log the user (with the new password) : ";
	if($babili_users->login('test@babili.net', md5('hello'))){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Test the user is logged : ";
	if($babili_users->babili_user_is_logged_in()){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Test to get the user id of the user : ";
	if($babili_users->babili_user_get_id()){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Test to get infos about the current user : ";
	$user_info = $babili_users->babili_user_get($user_id);
	if($user_info['email'] == 'test@babili.net'){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Test to get infos about the admin : ";
	$admin_info = $babili_users->babili_user_get(2);
	if($admin_info['id'] == 2){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	
	echo "<li>Test to get all users : ";
	if(count($babili_users->babili_user_get_all()) > 0){
		echo "<span style='color: green; font-weight:bold;'><span style='color: green; font-weight:bold;'>OK</span></span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Test to search the test user : ";
	if(count($babili_users->babili_user_search_result(array('email' => 'test@babili'))) > 0){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	
	echo "<li>Test Logout : ";
	$babili_users->babili_user_logout();
	if(!$babili_users->babili_user_is_logged_in()){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	
	echo "<li>Delete the user : ";
	if($babili_users->delete($user_id)){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}

echo '</ul>';


/* Permissions Tests*/
	
echo "<h2>Permissions TESTS : </h2>
<br />
<ul>";

/*
  	0 	ANONYMOUS 	Anonymous
	1 	CONTRIBUTOR 	Contributor
	2 	VALIDATOR 	Validator
	3 	ADMIN 	administrator
 	*/

	// Modify role
	echo "<li>Modify role : ";
	$result = $babili_permission->modify_role(0, "Anon");
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
		$babili_permission->modify_role(0, "Anonymous");
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	// Assign permission
	echo "<li>Assign permission : ";
	// assign administrator the member with the id 2
	$result = $babili_permission->assign_permission(2, 3);
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	// Remove permission
	echo "<li> Remove permission : ";
	$result = $babili_permission->remove_permission(2, 3);
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	
	// Assign validator
	echo "<li>Assign validator : ";
	$result = $babili_permission->assign_validator(2, 2);
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	// Remove validator
	echo "<li> Remove validator : ";
		$result = $babili_permission->remove_validator(2, 2);
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
echo "</ul>";

/*
 * Test Product
 * 
 */

echo "<h2>Product TESTS : </h2>
<br />
<ul>";

	echo "<li>Add a product : ";
	$last_id_product = $babili_product->add("Bonita BOS", "Première version", "5.3", "enable");
	if ($last_id_product){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Modify a product : ";
	$result = $babili_product->modify($last_id_product, "Bonita BOS", "first version", "5.4", "enable");
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Add a project to product_id ($last_id_product) : ";
	$last_id_project = $babili_product->add_project("Bonita Open Solution", "first version project", "5.4.1", "enable", array($last_id_product));
	if ($last_id_project){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Get a project ($last_id_product) : ";
	$project = $babili_product->get_project($last_id_project);
	if (project){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	
	echo "<li>Modify last project ($last_id_project) : ";
	$result = $babili_product->modify_project($last_id_project, "Bonita Open Solution!", "Modification BOS Project", "5.4.3", "enable", array($last_id_product));
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Add languages to a product ($last_id_product) : ";
	$result = $babili_product->add_languages($last_id_product, array(1,2));
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Remove languages to a product ($last_id_product) : ";
	$result = $babili_product->remove_languages($last_id_product, array(2));
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Get a product (49) and all his projects : ";
	$result = $babili_product->get(49);
	if ($result){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}
	
	echo "<li>Get all products and all its projects : ";
	$result = $babili_product->get_all();
	if ($result != NULL){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";
	}	
	

	echo "<h2>Translation TESTS</h2>
	</br>
	<ul>";
	
	echo "<li>Add file : ";
	$fileid = $babili_translation->add_file("/home/paul/project1", 1);
	if ($fileid){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}
	
	echo "<li>Add property key : ";
	$property_key = $babili_translation->add_property_key("key_1", "first key", $fileid);
	if ($property_key){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}	
	
	echo "<li>Add a translation : ";
	$translation_id = $babili_translation->add("value of first property key", $property_key, 1, 2, 2, "first comment as a contributor");
	if ($translation_id){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}
	
	echo "<li>Modify a translation : ";
	if ($babili_translation->modify($translation_id, "new value of property key", 2, 2, "second comment as a contributor", "first comment as a contributor")){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}
	
	echo "<li>Get a translation :";
	if ($babili_translation->get_translation($translation_id) != NULL){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}

	echo "<li>Get all translation : ";
	if ($babili_translation->get_all_translation($property_key, array(1,2))){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}
		
	echo "<li>Get all property keys : ";
	if ($babili_translation->get_all_property_keys(1, array(1,2))){
		echo "<span style='color: green; font-weight:bold;'>OK</span></li>";
	}else{
		echo "<span style='color: red; font-weight:bold;'>FAILED</span></li>";		
	}
	

	
	
echo "</ul>";